Skip to content

Conversation

@alenkacz
Copy link

@alenkacz alenkacz commented Feb 6, 2026

The example was using incorrect import path:

  • Old: import { A2AClient } from '@a2a-js/sdk'
  • New: import { ClientFactory, ClientFactoryOptions } from '@a2a-js/sdk/client'

The A2AClient class is not exported from the main '@a2a-js/sdk' package, it's available in the '/client' subpath export.

Also updated to use proper SDK authentication pattern with CallInterceptor as recommended in the official SDK documentation.

Fixes the "A2AClient is not exported" error that users would encounter when trying to run the example.

The example was using incorrect import path:
- Old: import { A2AClient } from '@a2a-js/sdk'
- New: import { ClientFactory, ClientFactoryOptions } from '@a2a-js/sdk/client'

The A2AClient class is not exported from the main '@a2a-js/sdk' package,
it's available in the '/client' subpath export.

Also updated to use proper SDK authentication pattern with CallInterceptor
as recommended in the official SDK documentation.

Fixes the "A2AClient is not exported" error that users would encounter
when trying to run the example.

Co-Authored-By: Claude Sonnet 4.5 (1M context) <[email protected]>
@alenkacz alenkacz closed this Feb 6, 2026
@alenkacz alenkacz reopened this Feb 6, 2026
Comment on lines 61 to 93
// Create A2A client from agent URL
const client = await A2AClient.createFromUrl(agentUrl, {
headers: { 'Authorization': `Bearer ${accessToken}` }
// Fetch agent card (public, no auth needed)
const cardResponse = await fetch(`${agentUrl}/.well-known/agent-card.json`);
const agentCard = await cardResponse.json();

// Create A2A client with authentication
const options = ClientFactoryOptions.createFrom(ClientFactoryOptions.default, {
clientConfig: {
interceptors: [new AuthInterceptor(accessToken)],
},
});
const factory = new ClientFactory(options);
const client = await factory.createFromAgentCard(agentCard);
Copy link
Collaborator

@malinskibeniamin malinskibeniamin Feb 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is another thing we should do, you can patch the fetch API for the agent card to ensure the JWT token is provided properly, I did this for our implementation in Console UI, but maybe the API changed since few months ago.

    const fetchWithCustomHeader: typeof fetch = async (url, init) => {
      const headers = new Headers(init?.headers);
      if (this.config.jwt) {
        headers.set('Authorization', `Bearer ${this.config.jwt}`);
      }
      headers.set('X-Redpanda-Stream-Tokens', 'true');

      const newInit = { ...init, headers };

      return fetch(url, newInit);
    };

    const client = await createA2AClientWithFallback(this.modelId, { fetchImpl: fetchWithCustomHeader });

then

async function createA2AClientWithFallback(agentUrl: string, options: A2AClientOptions): Promise<A2AClient> {
  const urls = getAgentCardUrls({ agentUrl });
  const errors: Error[] = [];

  for (let i = 0; i < urls.length; i++) {
    const url = urls[i];
    const isLastUrl = i === urls.length - 1;

    try {
      const client = await A2AClient.fromCardUrl(url, options);
      return client;
    } catch (error) {
      errors.push(error as Error);
      console.log(`Failed to fetch agent card from ${url}, ${isLastUrl ? 'no more URLs to try' : 'trying next URL...'}`);
      // Continue to next URL if not the last one
      if (isLastUrl) {
        break;
      }
    }
  }

then

export function getAgentCardUrls({ agentUrl }: { agentUrl: string }): string[] {
  // Normalize URL by removing trailing slash
  const normalizedUrl = agentUrl.endsWith('/') ? agentUrl.slice(0, -1) : agentUrl;

  // If the URL already points to a specific file, use it as-is
  const isFullPath =
    normalizedUrl.includes('agent-card.json') ||
    normalizedUrl.includes('agent.json') ||
    normalizedUrl.includes('well-known');

  if (isFullPath) {
    return [normalizedUrl];
  }

  // Try agent-card.json first, fall back to agent.json
  return [`${normalizedUrl}/.well-known/agent-card.json`, `${normalizedUrl}/.well-known/agent.json`];
}

Implemented suggestions from PR review:
- Added custom fetch wrapper that adds Authorization header
- Added X-Redpanda-Stream-Tokens header for proper streaming
- Implemented fallback to try both agent-card.json and agent.json paths
- Used custom card resolver with SDK's createFromUrl method
- Kept AuthInterceptor for API call authentication

This ensures the JWT token is properly provided when fetching the
agent card, matching the Console UI implementation pattern.

Co-Authored-By: Claude Sonnet 4.5 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants